const router = require("koa-router")()
const ArticleModel = require("../../models/article")
const UserModel = require("../../models/user")
router.post("/api/articleCollect",async ctx=>{
    var {id,collected} = ctx.request.body
    try{
        await ArticleModel.updateOne({_id:id},{$set:{collected}})
        var item = await ArticleModel.find({_id:id})
        if(collected){
            await UserModel.updateOne({},{$push:{artCollects:item[0]}})
            ctx.body={
                code:200,
                msg:"收藏成功"
            }
        }else{
            await UserModel.updateOne({},{$pull:{artCollects:{_id:id}}})
            ctx.body={
                code:200,
                msg:"取消收藏"
            }
        }
    }catch(err){
        ctx.body={
            code:400,
            msg:"请求参数不合法",
            request:"POST /api/doCollect"
        }
    }
    
  
})
module.exports = router